//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
    address:'',
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    temp: "4",
    temperature: "-1/10℃",
    type: "晴",
    city: "北京",
    date: "2000-01-01",
    direction: "无持续风向微风级",
    condition: '10',
    futureArray: '[]',
  },
  //事件处理函数
  bindViewTap: function () {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  toNews:function(){
    wx.navigateTo({ url: '../news/news' })
  },
  onLoad: function () {
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse) {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
    var that = this
    var result = null;
    wx.request({
      url: 'http://v.juhe.cn/weather/index',
      data: {
        "cityname": "德阳",
        'dtype':'josn',
        "key": "53003de42c76d5d11af5d6f462896efd"
      },
      success(res) {
        console.log(res)
        result = res.data.result;
        var test1 = JSON.stringify(result);
        // console.log(test1)
        that.setData({
          city: res.data.result.today.city,
          temp: res.data.result.today.temperature,
          temperature: res.data.result.sk.temp,
          type: res.data.result.sk.wind_strength,
          direction: res.data.result.sk.wind_direction,
          date: res.data.result.sk.time,
          futureArray: res.data.result.future,
        })
      }
    })
  },
  getUserInfo: function (e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
  getUserLocation:function(){
    let ad = this;
    wx.getSetting({
      success:(res)=>{
        console.log(JSON.stringify(res))
        // res.authSetting['scope.userLocation'] == undefined 初始化进入该页面
        // res.authSetting['scope.userLocation'] == false 非初始化进入该页面，且未授权
        // res.authSetting['scope.userLocation'] == true 地理位置授权
        if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true){
          wx.showModal({
            title: '请求授权当前位置',
            content: '需要获取您当前位置，请确认授权',
            success:function(res){
             if(res.cancel){
               wx.showToast({
                 title: '拒绝',
                icon:'none',
                duration:1000
               })
             } else if (res.confirm) {
               wx.openSetting({
                 success:function(dataAu){
                   if (dataAu.authSetting['scope.userLocation'] == true){
                     wx.showToast({
                       title: '授权成功',
                       icon: 'none',
                       duration: 1000
                     })
                    //  再次授权，调用wx.getLocation的API
                    wx.getLocation();
                   }else{
                     wx.showToast({
                       title: '授权失败',
                       icon: 'none',
                       duration: 1000
                     })
                   }
                 }
               })
             }
            }
          })
        } else if (res.authSetting['scope.userLocation'] == undefined){
          //  调用wx.getLocation的API
          wx.getLocation();
        }else{
          //  调用wx.getLocation的API
          wx.getLocation();
        }
      }
    })
  }, 
  getLocation: function () {
    let vm = this;
    wx.getLocation({
      type: 'gcj02',
      altitude: true,
      success: function (res) {
        console.log(JSON.stringify(res))
        var latitude = res.latitude
        var longitude = res.longitude
        var speed = res.speed
        var accuracy = res.accuracy;
        // vm.getLocal(latitude, longitude)
      },
      fail: function (res) {
        console.log('fail' + JSON.stringify(res))
      }
    })
  }
})